const app = getApp();
const api = require('../../utils/api.js');
Page({
	data: {
		listShow: [{
				title: "地点",
				radioItems: [],
				type:"address",
				show: false
			},
			{
				title: "年龄",
				radioItems: [],
				type:"age",
				show: false
			},
			{
				title: "遛点",
				radioItems: [],
				type:"number",
				show: false
			},
			{
				title: "课程分类",
				radioItems: [],
				type:"kctype",
				show: false
			}
		],
		kcType:app.search.kctype,
		liudian:app.search.liudian,
		age:app.search.age,
		address:app.search.address,
		childAddress:app.search.address,
	},
	onLoad: function() {

	},
	//根据类型查询标签
	getMateList(type,parentId,callback) {
		api.getMateList({
			data: {
				type: type,
				parentId:parentId,
				pageSize: "500",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					let list = [];
					res.data.list.rows.forEach(function(item) {
						if(item.type == type){
							let cheacked = false;
							console.log(app.search)
							//之前的结果再次进入选中
							if(app.search.kctype == item.value || app.search.age == item.value 
									|| app.search.liudian == item.value  || app.search.address == item.value ){
								cheacked = true;
							}
							let obj = {
								name: item.id,
								value: item.value,
								type:item.type,
								checked:cheacked,
								parentId: item.parentId,
							}
							list.push(obj);
						}
					});
					
					callback(list);
				}
			}
		});
	},
	//展示标签内容
	showContent: function(e) {
		let _this = this;
		var index = parseInt(e.currentTarget.dataset.index);
		var key = "listShow[" + index + "].show";
		var radioItems = "listShow[" + index + "].radioItems";
		var val = this.data.listShow[index].show;
		var type = this.data.listShow[index].type;
		if(!val && this.data.listShow[index].radioItems.length == 0){
			let parentId = null;
			if(type == "address"){
				parentId = 0;
			}
			this.getMateList(type,parentId,function(list){
				_this.setData({
					[key]: !val,
					[radioItems]: list,
				})
			});
		}else{
			_this.setData({
				[key]: !val
			})
		}
		
	},
	radioChange: function(e) {
		let _this = this;
		let value = e.detail.value;
		let values = value.split("_");

	    if(values[0] == "age"){
	    	this.setData({
				age:values[1]
			})
	    }else if(values[0] == "number"){
	    	this.setData({
				liudian:values[1]
			})
	    }else if(values[0] == "kctype"){
	    	this.setData({
				kctype:values[1]
			})
	    }else if(values[0] == "address"){
	    	this.setData({
				address:values[1]
			});
			if(values[2] != undefined){
				this.getMateList("address",values[2],function(list){
					_this.setData({
						childAddress:list
					});
				});
			}
	    	
	    }
	},
	//重置查询条件
	resetSearch:function(){
		let newData = this.data.listShow;
		newData.forEach(function(item){
			item.radioItems.forEach(function(obj){
				obj.checked = false;
			});
		});
		//二级选项
		let childAddress = this.data.childAddress;
		childAddress.forEach(function(obj){
			console.log(obj.checked);
			obj.checked = false;
		});
		this.setData({
			age:"",
			liudian:"",
			kctype:"",
			address:"",
			listShow:newData,
			childAddress:childAddress
		});
		app.search.age = "";
		app.search.liudian = "";
		app.search.kctype = "";
		app.search.address = "";
	},
	//根据条件查询
	search:function(){
		app.search.age = this.data.age;
		app.search.liudian = this.data.liudian;
		app.search.kctype = this.data.kctype;
		app.search.address = this.data.address;
		wx.switchTab({
			url: "../kclist/kclist"
		})
	},
});